package com.njitt.bysj.srevice;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njitt.bysj.base.BaseService;
import com.njitt.bysj.bean.BomBean;
import com.njitt.bysj.bean.ProjectDesignBean;
import com.njitt.bysj.mapper.BomBeanMapper;
import com.njitt.bysj.mapper.ProjectDesignBeanMapper;
import com.njitt.bysj.model.BomModel;
import com.njitt.bysj.query.BomQuery;
import com.njitt.bysj.query.ProjectDesignQuery;
import com.njitt.bysj.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: wyj
 * @time: 2022/4/15 18:34
 */
@Service
public class ProjectDesignService extends BaseService<ProjectDesignBean, Integer> {
    @Resource
    private ProjectDesignBeanMapper projectDesignBeanMapper;
    @Resource
    private BomBeanMapper bomBeanMapper;

    public Map<String, Object> selectByParams(ProjectDesignQuery query) {
        Map<String, Object> map = new HashMap<>(16);
        PageHelper.startPage(query.getPage(), query.getLimit());
        PageInfo<ProjectDesignBean> info = new PageInfo<>(projectDesignBeanMapper.selectByParams(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    public ProjectDesignBean selectProjectByProjectId(Integer projectId) {
        AssertUtil.isTrue(projectId == null, "指派失败");
        return projectDesignBeanMapper.selectProjectByProjectId(projectId);

    }

    public List<Map<String, Object>> selectDesignerByRoleInTechnology() {
        return null;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @SuppressWarnings("AlibabaTransactionMustHaveRollback")
    public void updateEmpByProjectDesign(ProjectDesignBean projectDesignBean) {
        AssertUtil.isTrue(projectDesignBean == null, "指派失败");
        AssertUtil.isTrue(projectDesignBean.getProjectId() == null, "指派失败");
        AssertUtil.isTrue(projectDesignBean.getDesignEmpId() == null, "指派失败");
        ProjectDesignBean projectDesignBean1 = projectDesignBeanMapper.selectByPrimaryKey(projectDesignBean.getId());
        AssertUtil.isTrue(projectDesignBean1 == null, "指派失败ID 为空");
        AssertUtil.isTrue(projectDesignBean.getDesignEmpId().equals(projectDesignBean1.getDesignEmpId()), "重复指派");
        projectDesignBean.setStatus(1);
        Integer integer = projectDesignBeanMapper.updateByPrimaryKeySelective(projectDesignBean);
        AssertUtil.isTrue(integer == null || integer == 0, "指派失败");
    }

    public Integer insetBatchList(List<BomBean> list) {
        /**
         * @description: excel 导入物料
         * @return:  * @param model
         * @author:
         * @time: 2022/4/17 13:27
         */
        return bomBeanMapper.insertBatch(list);
    }

    public void updateProjectDesignStatusByBom(Integer id) {
        /**
         * @description: 修改设计订单状态
         * @return:  * @param id
         * @author:
         * @time: 2022/4/17 13:27
         */
        AssertUtil.isTrue(id == null || id == 0, "提交物料单失败");
        ProjectDesignBean projectDesignBean = new ProjectDesignBean();
        projectDesignBean.setId(id);
        projectDesignBean.setStatus(2);
        Integer integer = projectDesignBeanMapper.updateByPrimaryKeySelective(projectDesignBean);
        AssertUtil.isTrue(integer == null || integer == 0, "提交失败");
    }

    public Map<String, Object> bomSelectByParams(BomQuery query) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        Map<String, Object> map = new HashMap<>(16);
        PageInfo<BomBean> info = new PageInfo<>(bomBeanMapper.selectByParams(query));
        map.put("code", 0);
        map.put("data", info.getList());
        map.put("count", info.getTotal());
        map.put("msg", "success");
        return map;
    }

    public Integer selectBomByProjectNoAndMaterialNo(String projectNo, String materialNo) {
        /**
         * @description: 查这个项目下的这个物料数量
         * @return:  * @param projectNo
         * @param materialNo
         * @author:
         * @time: 2022/4/21 10:57
         */
        AssertUtil.isTrue(StringUtils.isBlank(projectNo), "项目编号不存在");
        AssertUtil.isTrue(StringUtils.isBlank(materialNo), "物料编号不存在");
        Integer integer = bomBeanMapper.selectBomCountByProjectNoAndMaterialNo(projectNo, materialNo);
        AssertUtil.isTrue(integer == null || integer == 0, "物料不存在");
        return integer;
    }


}
